package LeetCodeHot100TopInterview;

public class Q169_MajorityNum {
    //出现最多的元素
    //摩尔投票法 ！！
    public int majorityElement(int[] nums) {

        int mole = 0;
        int ans = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if (ans == nums[i]){
                mole++;
            }else {
                mole = Math.max(0, --mole);
                if (mole == 0){
                    ans = nums[i];
                    mole = 1;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Q169_MajorityNum  fun = new Q169_MajorityNum();
        System.out.println(fun.majorityElement(new int[]{1, 3, 1, 1, 4, 1, 1, 5, 1, 1, 6, 2, 2}));
    }
}
